package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/16 11:10
 */
//后台系统-->查询用户
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    /*
    管理后台-->获取当前登录用户
     */
    @RequestMapping("getUsername")
    public Result getUsername() {
        //当spring-security完成认证后,
        //会将当前用户信息保存到框架提供的
        //上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user != null) {//执行获取登录用户存在
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(queryPageBean);
        //返回分页结果对象
        return pageResult;
    }



    /*
     用户管理-->新建添加套餐
      */
    @RequestMapping("/add")

    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        //程序健壮性和安全性处理
        try {
            //调用userService完成添加
            userService.add(user,roleIds);

            return new Result(true,"新增用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增失败");
        }
    }


    @RequestMapping("/findById")
    public Result findById(Integer id){
        //程序健壮性和安全性处理
        try {
            //根据id查询对应id的数据
            com.itheima.pojo.User user = userService.findById(id);
            //查询对应id的用户数据成功后返回
            return new Result(true,"查询成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            //查询对应id的用户数据失败后返回信息
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        //程序健壮性和安全性处理
        try {
            //调用userService完成查询
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
   // @PreAuthorize("hasRole(ROLE_HEALTH_MANAGER)")//权限控制
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        //程序健壮性和安全性处理
        try {
            //调用userService完成查询
            userService.edit(user,roleIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/delete")
   // @PreAuthorize("hasAuthority('')")
    public Result delete(Integer id){
        //程序健壮性和安全性处理
        try {
            //调用userService完成删除
            userService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

}
